<template>
  <div class="adminqianming">
    <admin_top></admin_top>
    <div style="padding: 0 20px;">
      <el-button type="danger" @click="addchognzhibtn" style="margin-bottom: 20px;">新建充值</el-button>
      <el-table :data="tableData" style="text-align: center;" align="center" border >
        <el-table-column label="创建时间">
          <template slot-scope="scope">
            <span>{{ scope.row.created_at }}</span>
          </template>
        </el-table-column>
        <el-table-column label="充值商家">
          <template slot-scope="scope">
            <span>{{ scope.row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="充值金额">
          <template slot-scope="scope">
            <span>{{ scope.row.amount }}</span>
          </template>
        </el-table-column>
        <el-table-column label="充值状态">
          <template slot-scope="scope">
            <span v-if="scope.row.status==0">待审核</span>
            <span v-if="scope.row.status==1">已充值</span>
          </template>
        </el-table-column>
        <el-table-column label="备注">
          <template slot-scope="scope">
            <span>{{ scope.row.remark }}</span>
          </template>
        </el-table-column>
        <el-table-column label="打款截图">
          <template slot-scope="scope">
            <img v-if="scope.row.payment_prove" :src="scope.row.payment_prove" alt="" @click="openimg(scope.row.payment_prove)" style="width: 100px;">
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button v-if="scope.row.status==0" type="text" @click="shenhe(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- <div style="text-align: center;margin-top: 10px;padding-bottom: 10px;">
        <el-pagination
          @current-change="pagechange"
          background
          layout="prev, pager, next"
          :page-size="all.page_size"
          :total="total">
        </el-pagination>
      </div> -->
    </div>
    <!-- 新建充值 -->
    <el-dialog
      title="新建线下充值"
      :visible.sync="chongzhishow"
      width="500px">
        <el-form  label-width="80px">
          <!-- <el-form-item label="充值商家">
            <el-input @blur="shopname" style="width: 350px;" v-model="shop_name"  placeholder="输入店铺名"></el-input>
          </el-form-item> -->
          <el-form-item label="充值商家" >
            <el-select style="width: 350px;" 
            filterable
            remote 
            clearable
            v-model="upform.shop_id" 
            :remote-method="remoteMethod"
            placeholder="请选择">
              <el-option
              v-for="item in options"
              :key="item.user_id"
              :label="item.name"
              :value="item.user_id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="充值金额">
            <el-input @input="numberFn('amount')" style="width: 350px;" v-model="upform.amount"  placeholder="支持到两位小数"></el-input>
          </el-form-item>
          <el-form-item label="备注">
            <el-input style="width: 350px;" type="textarea" v-model="upform.remark"></el-input>
          </el-form-item>
          <el-form-item label="打款截图">
            <chuantu v-if="chongzhishow"  ref="chuantu" @getimg="getimg"></chuantu>
          </el-form-item>
        </el-form>
        <div style="text-align: center;">
          <el-button type="primary" @click="upfubu">确定发布
          </el-button>
        </div>
    </el-dialog>
    <el-dialog
      title="查看图片"
      :visible.sync="showimg"
      width="500">
      <img :src="nowimg" style="width: 100%;">
    </el-dialog>
  </div>
</template>

<script>
  import admin_top from './top.vue';
  import axios from 'axios'
  import chuantu from "../zujian/chuantu.vue";
import { reactive } from 'vue';
  export default {
    components: {
      admin_top,chuantu
    },
    name: 'adminqianming',
    data() {
      return {
        all: {
          page_size: 20,
          page: 1,
        },
        tableData: "",
        total:0,
        chongzhishow:false,
        upform:{
          shop_id:'',
          amount:null,
          remark:'',
          payment_prove:''
        },
        // shop_name:'',
        options:[],
        showimg:false,
        nowimg:''
      }
    },
    watch: {

    },
    computed: {

    },
    created() {
      this.getlist()
    },
    methods: {
      //打开图片
      openimg(url){
        this.nowimg = url
        // this.nowimg = 'http://test91.91haoka.cn/1679368811Nj7.jpg'
        this.showimg = true
      },
      getimg:function(img){
        console.log(img)
        this.upform.payment_prove = img
      },
      //搜索店铺
      // shopname(){
      //   axios.get(`/api/plan-market/admin/shop/info?name=${this.shop_name}`)
      // 		.then(response => {
      // 			if (response.data.msg.code == 0) {
      //         console.log(response.data.data)
      //         this.upform.shop_id = response.data.data.user_id
      // 			} else {
      // 				this.$message.error(response.data.msg.info);
      // 			}
      // 		});
      // },
      //商家搜索下拉
      remoteMethod(query){
        if (query !== '') {
          console.log(query)
              axios.get(`/api/plan-market/admin/shop/info?name=${query}&flag=1`)
      		.then(response => {
      			if (response.data.msg.code == 0) {
              console.log(response.data.data)
              this.options = response.data.data
      			} else {
      				this.$message.error(response.data.msg.info);
      			}
      		});
        } else {
          this.options = [];
        }
      },
      //获取全部
      getlist: function() {
        var that = this
        axios.get('/api/plan-market/admin/offline/recharge/list')
          .then(response => {
            if (response.data.msg.code == 0) {
              console.log(response.data)
              this.tableData = response.data.data
              // this.total = response.data.data.total
            } else {
              this.$message.error(response.data.msg.info);
            }
          })
      },
      
      //翻页
      pagechange(val){

        this.all.page = val
        this.getlist()
      },
      
      //新建
      addchognzhibtn(){
        this.chongzhishow = true
        this.upform.shop_id = ''
        this.upform.amount = ''
        this.upform.remark = ''
        this.upform.payment_prove = ''
        this.options = []
        // this.shop_name = ''
      },
      //限制两位小数
      numberFn(p){
          this.upform[p] = this.upform[p]
          .replace(/[^\d^\.]+/g, '')  
          .replace(/^0+(\d)/, '$1')
          .replace(/^\./, '0.')
          .match(/^\d*(\.?\d{0,2})/g)[0] || ''
      },
      //提交
      upfubu(){
        this.$confirm('是否确认提交?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          axios.post('/api/plan-market/admin/offline/recharge/create',this.upform)
          .then(response => {
            if (response.data.msg.code == 0) {
              console.log(response.data)
              this.$message({
                type: 'success',
                message: '操作成功!'
              });
              
              this.getlist()
            } else {
              this.$message.error(response.data.msg.info);
            }
          })
          this.chongzhishow = false
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });          
        });
        
      },
      priceInput(obj) {
        // 清除数字和点以外的字符
        obj.value = obj.value.replace(/[^\d.]/g, "");
        // 验证第一个字符是否是数字，也就是是第一个字符不能是点
        obj.value = obj.value.replace(/^\./g, "");
        // 只保留第一个点, 清除多余的点
        obj.value = obj.value.replace(/\.{2,}/g, ".");
        obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
        // 只能输入两个小数
        obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3');

        // 此处控制的是如果没有小数点，首位不能为0，类似于01、02的金额
        if (obj.value.indexOf(".") < 0 && obj.value != "") {
          if (obj.value.substr(0, 1) == '0' && obj.value.length == 2) {
            obj.value = parseFloat(obj.value);
          }
        } else { // 此处控制的是如果有小数点，整数如果大于等于两位，第一位不能为0，类似于01.16、02.77的金额
          let val = obj.value,
            beforePoint = val.split('.')[0],
            afterPoint = val.split('.')[1];
          if (beforePoint.length >= 2 && beforePoint[0] == 0) {
            obj.value = `${beforePoint.substring(1)}.${afterPoint}`;
          }
        }
      },
      //删除
      shenhe(id){
        this.$confirm('是否确认删除', '再次确认', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          axios.post('/api/plan-market/admin/offline/recharge/delete',{
            id:id
          })
            .then(response => {
              if(response.data){
                this.$message({
                  type: 'success',
                  message: '操作成功!'
                });
                this.getlist()
              }
              
            })
          
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });          
        });
      }

    },

  }
</script>

<style scoped>
  .adminqianming {
    background: #EAEAEA;
    min-height: 100vh;
    padding: 0px;
  }
</style>
